package com.csdj.t309springboot.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * mybaits plus 自动生成代码配置
 */
@Configuration
public class MyBatisGeneratorConfig {
    public static void main(String[] args) {
        //项目的当前的路径
        String projectPath = System.getProperty("user.dir");
        Map<OutputFile,String> paths = new HashMap<>();
        paths.put(OutputFile.mapper,projectPath.concat("/src/main/java/com/csdj/t309springboot/mapper"));
        paths.put(OutputFile.entity,projectPath.concat("/src/main/java/com/csdj/t309springboot/entity"));
        paths.put(OutputFile.mapperXml,projectPath.concat("/src/main/java/com/csdj/t309springboot/mapper"));
        paths.put(OutputFile.serviceImpl,projectPath.concat("/src/main/java/com/csdj/t309springboot/service"));
        paths.put(OutputFile.service,projectPath.concat("/src/main/java/com/csdj/t309springboot/service"));
        paths.put(OutputFile.controller,projectPath.concat("/src/main/java/com/csdj/t309springboot/controller"));
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/springcloud", "admin", "123456")
                .globalConfig(builder -> {
                    builder.author("cloud") // 设置作者
                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir()
                            .outputDir(projectPath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.csdj.t309springboot") // 设置父包名
//                            .moduleName("") // 设置父包模块名
                            .serviceImpl("service")
                            .pathInfo(paths); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("user_login_record") // 设置需要生成的表名
                            .controllerBuilder().enableRestStyle()//配置restController
                            .mapperBuilder().enableBaseResultMap().enableBaseColumnList()
                            .serviceBuilder().formatServiceImplFileName("%sService");

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}
